/*
    Project:        Home Power Control
    File:           HPCSensorValue.cpp
    Author:         christian@karunamusic.at
    Date:           01.2015
    Description:
 */

#include "Logger/HPCLogger.h"
#include "HPCSensorValue.h"

HPCSensorValue::HPCSensorValue()
    : HPCValue()
{
}

HPCSensorValue::HPCSensorValue( const QVariant& val )
    : HPCValue(val)
{
}

HPCSensorValue::HPCSensorValue( HPCSensorValue& ref )
    : HPCValue()
{
    *this = ref;
}

HPCSensorValue::~HPCSensorValue()
{
    qDeleteAll( m_Min.begin(),m_Min.end() );
    m_Min.clear();
    qDeleteAll( m_Max.begin(),m_Max.end() );
    m_Max.clear();
}

const HPCValue* HPCSensorValue::Min( int ndx )
{
    if ( ndx < m_Min.size() )
    {
        return m_Min[ndx];
    }

    return 0;
}

const HPCValue* HPCSensorValue::Max( int ndx )
{
    if ( ndx < m_Max.size() )
    {
        return m_Max[ndx];
    }

    return 0;
}

void HPCSensorValue::addAlarm( const QString& Min, const QString& Max )
{
    QVariant v(m_Var.type());

    if ( !Min.isNull() && !Min.isEmpty() )
    {
        v = Min;
        m_Min.append( new HPCAlarmValue(v) );
    }
    if ( !Max.isNull() && !Max.isEmpty() )
    {
        v = Max;
        m_Max.append( new HPCAlarmValue(v) );
    }
}

HPCSensorValue& HPCSensorValue::operator = ( const HPCValue& v )
{
    HPCValue::operator= ( v );
    int a = isAlarm();
    if ( a != 0 )
    {
        bool gt = true;
        if ( a < 0 )
        {
            a *= -1;
            gt = false;
        }
        a--;
        LOG(trace, "value alarmed");
        emit alarmed( *this, gt, a );
    }
    return *this;
}

int HPCSensorValue::isAlarm()
{
    int ret = 0;

    for ( int i = 0; i < m_Min.size(); i++ )
    {
        if ( this->V() <= m_Min[i]->V() )
        {
            if ( !m_Min[i]->Alarmed() )
            {
                ret = (i+1) * -1;
                m_Min[i]->Alarmed(true);
            }
        }
        else
        {
            m_Min[i]->Alarmed(false);
        }
    }

    for ( int j = 0; j < m_Max.size(); j++ )
    {
        if ( this->V() >= m_Max[j]->V() )
        {
            if ( !m_Max[j]->Alarmed() )
            {
                ret = j+1;
                m_Max[j]->Alarmed(true);
            }
        }
        else
        {
            m_Max[j]->Alarmed(false);
        }
    }

    return ret;
}

QDataStream& operator<< ( QDataStream& os, const HPCSensorValue& v )
{
    int len, i;

    len = v.m_Min.size();
    os << len;
    for ( i = 0; i < len; i++ )
    {
        LOG(info,"alarm min: " << v.m_Min[i]->V().toDouble());
        os << v.m_Min[i]->V().toDouble();
    }

    len = v.m_Max.size();
    os << len;
    for ( i = 0; i < len; i++ )
    {
        LOG(info,"alarm max: " << v.m_Max[i]->V().toDouble());
        os << v.m_Max[i]->V().toDouble();
    }

    return os;
}

QDataStream& operator>> ( QDataStream& is, HPCSensorValue& v )
{
    int len, i;

    is >> len;
    for ( i = 0; i < len; i++ )
    {
        double va;
        is >> va;
        if ( va )
        {
            QVariant var = va;
            LOG(info,"alarm min: " << var.toString());
            v.m_Min.append(new HPCAlarmValue(var));
        }
    }

    is >> len;
    for ( i = 0; i < len; i++ )
    {
        double va;
        is >> va;
        if ( va )
        {
            QVariant var = va;
            LOG(info,"alarm max: " << var.toString());
            v.m_Max.append(new HPCAlarmValue(var));
        }
    }

    return is;
}
